package com.drinks.yiniang.adapter.shop;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.base.BaseMultiItemQuickRCVAdapter;
import com.drinks.yiniang.common.listener.OnCountChangeListener;
import com.drinks.yiniang.common.listener.OnCounterClickListener;
import com.drinks.yiniang.entity.goods.GoodsJson;
import com.drinks.yiniang.entity.shop.ShoppingCartJson;
import com.drinks.yiniang.ui.widget.layout.ShoppingCartCounterLayout;
import com.drinks.yiniang.ui.widget.textview.TagTextView;
import com.drinks.yiniang.utils.GlideUtils;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.ui.widget.layout.AutoLineFeedLayout;
import com.hansen.library.utils.CommonUtils;
import com.hansen.library.utils.StringUtils;

import java.util.List;

import static com.drinks.yiniang.entity.shop.ShoppingCartJson.ITEM_TYPE_CART_GOODS;
import static com.drinks.yiniang.entity.shop.ShoppingCartJson.ITEM_TYPE_CART_RECOMMEND;
import static com.drinks.yiniang.entity.shop.ShoppingCartJson.ITEM_TYPE_CART_RECOMMEND_TITLE;

/**
 * 购物车adapter
 *
 * @author han
 * @date 2019/8/30
 */
public class ShoppingCartAdapter extends BaseMultiItemQuickRCVAdapter<ShoppingCartJson.ShoppingCartList, BaseViewHolder> {

    private TagTextView mTagTextView;

    private StringBuilder mBuilder;

    private OnCounterClickListener onCounterClickListener;
    private OnListCountChangeListener onListCountChangeListener;

    private Drawable mCheckDrawable;
    private Drawable mNormalDrawable;

    public ShoppingCartAdapter(Context mContext, @Nullable List<ShoppingCartJson.ShoppingCartList> data) {
        super(data);

        addItemType(ITEM_TYPE_CART_GOODS, R.layout.item_shopping_cart);
        addItemType(ITEM_TYPE_CART_RECOMMEND_TITLE, R.layout.item_cart_recommend_title);
        addItemType(ITEM_TYPE_CART_RECOMMEND, R.layout.item_home_goods);

        mCheckDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_checkbox_selected);
        mNormalDrawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_checkbox_normal);
    }

    @Override
    protected void convert(final BaseViewHolder helper, ShoppingCartJson.ShoppingCartList item) {
        switch (helper.getItemViewType()) {
            case ITEM_TYPE_CART_GOODS://购物车的商品
                //封面
                GlideUtils.loadGoodsList(mContext, (RatioRoundImageView) helper.getView(R.id.iv_shopping_cart_goods_cover), item.getCover_url());
                //名称
                helper.setText(R.id.tv_shopping_cart_goods_name, item.getGoods_name());
                //推荐语
                helper.setText(R.id.tv_shopping_cart_goods_slogan, item.getRecommend_reason());
                //价格
                mBuilder = new StringBuilder();
                mBuilder.append("¥");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getItem_price()));
                helper.setText(R.id.tv_shopping_cart_goods_price, mBuilder);
                //属性
                mBuilder = new StringBuilder();
                mBuilder.append(StringUtils.judgeGreaterNumber(item.getSingle_capacity(), 0f) ? item.getSingle_capacity() : "");
                mBuilder.append(mBuilder.length() > 0 ? "ml" : "");
                helper.setText(R.id.tv_shopping_cart_goods_volume, mBuilder);
                //数量
                ShoppingCartCounterLayout ll_shopping_cart_counter = helper.getView(R.id.ll_shopping_cart_counter);
                ll_shopping_cart_counter.setTextCount(StringUtils.getInt(item.getNum()));
                ll_shopping_cart_counter.setOnCountChangeListener(new OnCountChangeListener() {
                    @Override
                    public void onMinusCount(int count) {
                        if(onListCountChangeListener != null) {
                            onListCountChangeListener.onListCountChange(helper.getLayoutPosition() - getHeaderLayoutCount(), count);
                        }
                    }

                    @Override
                    public void onPlusCount(int count) {
                        if(onListCountChangeListener != null) {
                            onListCountChangeListener.onListCountChange(helper.getLayoutPosition() - getHeaderLayoutCount(), count);
                        }
                    }
                });

                ll_shopping_cart_counter.setOnCounterClickListener(new OnCounterClickListener() {
                    @Override
                    public void onCounterClick(int position) {
                        if (onCounterClickListener != null) {
                            onCounterClickListener.onCounterClick(helper.getLayoutPosition() - getHeaderLayoutCount());
                        }
                    }
                });

                //是否选中
                if(item.isChecked()) {
                    helper.setImageDrawable(R.id.iv_shopping_cart_choose, mCheckDrawable);
                } else {
                    helper.setImageDrawable(R.id.iv_shopping_cart_choose, mNormalDrawable);
                }

                break;
            case ITEM_TYPE_CART_RECOMMEND://推荐的商品
                GlideUtils.loadGoodsList(mContext, (RatioRoundImageView) helper.getView(R.id.iv_home_goods_cover), item.getCover_url());

                //活动tag
                addLabelTag((AutoLineFeedLayout) helper.getView(R.id.ll_home_goods_label), item.getActivity());

                //商品名称
                helper.setText(R.id.tv_home_goods_name, item.getName());
                //推荐语
                helper.setText(R.id.tv_home_goods_slogan, item.getRecommend_reason());
                //价格
                mBuilder = new StringBuilder();
                mBuilder.append("¥");
                mBuilder.append(StringUtils.getNullEmptyConvert__(item.getApp_price()));
                helper.setText(R.id.tv_home_goods_price, mBuilder);

                helper.addOnClickListener(R.id.iv_home_goods_add_cart);
                break;
            default:
                break;

        }
    }

    private void addLabelTag(AutoLineFeedLayout layout, List<GoodsJson.GoodsActivity> actList) {
        layout.removeAllViews();

        if (CommonUtils.isEmptyList(actList)) {
            layout.setVisibility(View.GONE);

            return;
        }

        layout.setVisibility(View.VISIBLE);

        for (GoodsJson.GoodsActivity data : actList) {
            mTagTextView = new TagTextView(mContext);

            mTagTextView.setTagColor(data.getTag_color());
            mTagTextView.setText(data.getTag_name());

            layout.addView(mTagTextView);
        }
    }

    public void setOnCounterClickListener(OnCounterClickListener onCounterClickListener) {
        this.onCounterClickListener = onCounterClickListener;
    }

    public void setOnListCountChangeListener(OnListCountChangeListener onListCountChangeListener) {
        this.onListCountChangeListener = onListCountChangeListener;
    }

    public interface OnListCountChangeListener {
        void onListCountChange(int pos, int count);
    }
}
